<template>
  <div class="deliveryinfo">
    <div class="header">
      <van-image
        class="logo"
        width="50"
        height="50"
        fit="contain"
        :src="require('@/assets/logo.jpg')"
      />
      
      <urlTags class="url-tags"></urlTags>
    </div>
    <div class="top_buttom">
      <div class="left_box">
        <van-cell :title="cityName || '请选择'" is-link arrow-direction="down" @click.stop.prevent="cityShow=true" />
      </div>
      <div class="right_box">
        <van-search
          v-model="searchText"
          show-action
          placeholder="请输入搜索关键词"
          @search="onSearch"
        >
        <template #action>
          <div @click="onSearch">搜索</div>
        </template>
        </van-search>
      </div>
    </div>
    <div class="orderMessage"> 提示：请选择您所在城市，输入详细地址查询是否在免费配送范围！</div>
    <MapContainer :deptId="deptId"/>
    <van-action-sheet v-model="cityShow" title="配送城市" style="height: 50%;">
      <div class="address">
        <van-search
          v-model="cityFilter"
          placeholder="请输入搜索关键词"
        >
        </van-search>
        <van-radio-group v-model="deptId">
          <van-radio v-for="item in filterCityList" :key="item.id" :name="item.id" checked-color="#ee0a24" @click.stop.prevent="onSelect(item)">{{item.deptName}}</van-radio>
        </van-radio-group>
      </div>
    </van-action-sheet>
    <van-action-sheet v-model="addressShow" title="配送地址" style="height: 50%;">
      <div class="address-list">
        <div class="address-item" v-for="item in addressList" :key="item.id" @click="onAddressSelect(item)">
          <div class="name"><i class="el-icon-location-information"></i>{{item.name}}</div> 
          <div class="address-text">{{item.address}}</div> 
          <div class="pname">{{item.pname+item.cityname+item.adname}}</div> 
        </div>
      </div>
    </van-action-sheet>
    <statementDialog ref="statementDialog"></statementDialog>
  </div>
</template>

<script>
import { getDeptList, isRange } from '@/api/api.js'
import MapContainer from '../../components/MapContainer.vue'
import { skipShop } from '@/helper/url'
import urlTags from '@/components/urlTags'
import { Dialog } from 'vant'
import statementDialog from './statementDialog'

export default {
  name: 'Home',
  data(){
    return {
      cityName:'',
      cityList:[],
      cityShow: false,
      deptId: '',
      searchText: '',
      addressList: [],
      addressShow: false,
      cityFilter: ''
    }
  },
  computed: {
    filterCityList() {
      return this.cityList.filter(item => item.deptName.includes(this.cityFilter))
    }
  },
  components:{
    MapContainer,
    urlTags,
    statementDialog,
  },
  mounted(){
    this.getDeptList()
  },
  methods:{
    getDeptList(){
      let data = {
        deliveryWay: 2
      }
      getDeptList(data).then(res=>{
        // console.log(res)
        if(res.code == 200) {
          this.cityList = res.data
        }
      })
    },
    onSelect(item) {
      // 默认情况下点击选项时不会自动收起
      // 可以通过 close-on-click-action 属性开启自动收起
      this.cityShow = false;
      this.cityName = item.deptName
    },
    onSearch() {
      this.getMapaddress(this.searchText)
    },
    onCancel() {
      // Toast('取消');
    },
    //根据城市获取地图位置
    getMapaddress(searchText) {
      var MSearch;
      AMap.plugin(["AMap.PlaceSearch"], () => {
        MSearch = new AMap.PlaceSearch({
          //构造地点查询类
          city: this.cityName, //城市
          extensions: 'all',
          citylimit: true,  //是否强制限制在设置的城市内搜索
        });
        MSearch.search(searchText,(status, result)=>{
          console.log(result)
          this.addressList = result.poiList && result.poiList.pois.map(item=>{
            item.subname = item.address
            return item
          })
          this.addressShow = true
        }); //关键字查询
      });
    },
    onAddressSelect(item){
      this.addressShow = false
      // console.log(item)
      this.isRange(item)
    },
    //是否在配送范围
    isRange(item){
      const unskip = this.$route.query && this.$route.query.unskip
      let data = {
        location: item.location.lng + ',' + item.location.lat,
        deptId: this.deptId,
        cityName: this.cityName,
      }
      isRange(data).then(res=>{
        //code 200 !=200就是一定不能配送
        if(res.code ==200){
          if(res.data == -1) {
            Dialog.alert({ 
              message: '非常抱歉，您选择的地址不在我们配送范围，敬请期待...',
              theme: 'round-button',
              // showCancelButton: true,
            });
            return
          }else if(res.data == 1) {
            Dialog.alert({ 
              message: `太棒啦，该区域有熊猫跳舞服务哦！${unskip ? '' : '点击立即下单！'}`,
              theme: 'round-button',
              showCancelButton: true,
              confirmButtonText: unskip ? '' : '立即下单' }).then(() => {
                !unskip && skipShop()
              }).catch(() => {
                // on cancel
              });
          }else{
            Dialog.alert({ 
              message: '主人！该地址蛋糕将由第三方配送上门，暂无法提供熊猫上门跳舞服务哦。',
              theme: 'round-button',
              showCancelButton: true,
              confirmButtonText: '立即下单' }).then(() => {
                !unskip && skipShop()
              }).catch(() => {
                // on cancel
              });
          }
        }else{
          Dialog.alert({ 
            message: '非常抱歉，您选择的地址不在我们配送范围，敬请期待...',
            theme: 'round-button', });
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.deliveryinfo {
  padding: 5px;
  .header {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 20px;
    .url-tags {
      margin-left: 20PX;
    }
  }
  .logo {
    ::v-deep .van-image__img {
      border-radius: 50%;
    }
  }
  .address{
    padding: 16px 16px 20px;
    height: calc(100% - 48px);
    .van-radio-group {
      max-height: calc(100% - 24px);
      overflow: auto;
    }
    .van-radio {
      margin-bottom: 15px;
      .van-radio__label{
        font-size: 16px;
      }
    }
  }
  .orderMessage {
    margin-bottom: 10px;
    padding: 0 10px;
    font-size: 14px;
    color: red;
    line-height: 18px;
    text-align: left;
  }
}
.top_buttom {
  margin-top: 5px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
  .van-cell {
    width: 80px;
    line-height: 40px;
    height: 40px;
    text-align: center;
    padding: 0!important;
    box-sizing: border-box;
  }
  .van-cell__right-icon{
    line-height: 40px;
    margin-right: 10px;
  }
  .left_box {
    width: 20%;
    // height: 20px;
    // height: 100%;
  }
  .right_box{
    width: 80%;
    height: 100%;
    .van-search{
      padding: 0;
      .van-search__content{
      }
    }
  }
}

.address-list {
  .address-item {
    text-align: left;
    background: #f8f8f8;
    padding: 6px 12px;
    margin: 10px;
    font-size: 14px;
    .name {
      font-weight: bold;
      font-size: 16px;
    }
    .address-text {
      margin: 4px;
    }
    .pname {
      margin-left: 4px;
      font-size: 12px;
      color: #999;
    }
  }
}

</style>